using System.Windows.Forms;

namespace EIM_Accounting
{
    /// <summary>
    /// 合同 合同管理 合同成员编辑
    /// </summary>
    public partial class CP010106 : Form
    {
        BLL.CP010106 cp010106 = new BLL.CP010106();

        public CP010106()
        {
            InitializeComponent();
        }

        private void input_float_Press(object sender, System.Windows.Forms.KeyPressEventArgs e)
        {
            if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8)
            {
                e.Handled = true;
            }

            if (e.KeyChar == 46)                       //小数点
            {
                string txtValue = ((System.Windows.Forms.TextBox)sender).Text;

                if (txtValue.Length <= 0)
                    e.Handled = true;           //小数点不能在第一位
                else
                {
                    float f;
                    float oldf;
                    bool b1 = false, b2 = false;
                    b1 = float.TryParse(txtValue, out oldf);
                    b2 = float.TryParse(txtValue + e.KeyChar.ToString(), out f);
                    if (b2 == true)
                    {
                        if (b1 == false)
                            e.Handled = true;
                        else
                            e.Handled = false;
                    }
                }
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            if (comboBox1.SelectedValue != null)
            {
                label3.Text = cp010106.getContractInfobyId(comboBox1.SelectedValue.ToString());
            }
        }

        private void CP010106_Load(object sender, System.EventArgs e)
        {
            comboBox1.DataSource = cp010106.getContractList().Tables[0];

            comboBox1.DisplayMember = "SignComp";
            comboBox1.ValueMember = "ID";

            if (comboBox1.Items.Count < 1)
            {
                btnSave.Enabled = false;
            }
            else
            {
                label3.Text = cp010106.getContractInfobyId(comboBox1.SelectedValue.ToString());
            }

            comboBox2.DataSource = cp010106.getMemberList().Tables[0];
            comboBox2.DisplayMember = "Name";
            comboBox2.ValueMember = "ID";

            if (comboBox2.Items.Count < 1)
            {
                btnSave.Enabled = false;
            }

            this.dateTimePicker1.CustomFormat = "   ";
            this.dateTimePicker2.CustomFormat = "   ";
        }

        private void btnClose_Click(object sender, System.EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, System.EventArgs e)
        {
            Accounting.Model.ContractMember contractMember = new Accounting.Model.ContractMember();

            contractMember.ContractID = int.Parse(comboBox1.SelectedValue.ToString());
            contractMember.StaffID = int.Parse(comboBox2.SelectedValue.ToString());
            if (dateTimePicker1.CustomFormat != "   ")
            {
                contractMember.EnterDate = dateTimePicker1.Value.ToString("yyyyMMdd");
            }
            if (dateTimePicker1.CustomFormat != "   ")
            {
                contractMember.ExitDate = dateTimePicker2.Value.ToString("yyyyMMdd");
            }
            contractMember.Remark = textBox4.Text;
            if (textBox2.Text.Trim() != string.Empty)
            {
                contractMember.SingleMoney = decimal.Parse(textBox2.Text);
            }

            cp010106.saveContractMember(contractMember);
            this.Close();
        }

        private void dateTimePicker_ValueChanged(object sender, System.EventArgs e)
        {
            ((DateTimePicker)sender).CustomFormat = "yyyy 年 MM 月 dd 日";
        }

        private void dateTimePicker_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Back)
            {
                ((DateTimePicker)sender).CustomFormat = "   ";
            }
        }
    }
}